package com.wuzhun.wxmp.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuzhun.wxmp.annotation.PageLoginRequired;
import com.wuzhun.wxmp.controller.BaseController;
import com.wuzhun.wxmp.model.dto.OperateLogDTO;
import com.wuzhun.wxmp.model.entity.OperateLog;
import com.wuzhun.wxmp.service.IOperateLogService;
import com.wuzhun.wxmp.util.DateTimeUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * 后台日志
 *
 * @author Paulin
 * @date 2023/7/31 9:45
 */
@Controller
@RequestMapping("/admin/log")
public class AdminLogController extends BaseController {
    @Resource
    private IOperateLogService operateLogService;

    @GetMapping("")
    @ApiOperation("消息列表页面")
    @PageLoginRequired
    public ModelAndView index(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                              @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize,
                              @RequestParam(name = "keyword", required = false) String keyword,
                              @RequestParam(name = "startDate", required = false) String startDate,
                              @RequestParam(name = "endDate", required = false) String endDate) {
        ModelAndView modelAndView = new ModelAndView();
        Page<OperateLog> pageOperateLog = new Page<>();
        pageOperateLog.setCurrent(page);
        pageOperateLog.setSize(pageSize);
        QueryWrapper<OperateLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(keyword)) {
            queryWrapper.like("ip", keyword).or().like("url", keyword);
        }
        modelAndView.addObject("keyword", keyword);
        if (StringUtils.isNotEmpty(startDate)) {
            queryWrapper.ge("created_time", DateTimeUtil.string2Date(startDate + " 00:00:00"));
        }
        modelAndView.addObject("startDate", startDate);
        if (StringUtils.isNotEmpty(endDate)) {
            queryWrapper.le("created_time", DateTimeUtil.string2Date(endDate + " 23:59:59"));
        }
        modelAndView.addObject("endDate", endDate);
        queryWrapper.orderByDesc("created_time");
        Page<OperateLogDTO> operateLogDTOPage = operateLogService.listByPage(pageOperateLog, queryWrapper);
        modelAndView.addObject("pageData", operateLogDTOPage);
        return commonView(modelAndView, "admin/log/index");
    }
}
